﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using deviceManage.Models;
using deviceManage.vo;
using deviceManage.Filters;

namespace deviceManage.Controllers
{
    /// <summary>
    /// 部门管理控制器
    /// </summary>
    [CheckLoginAttribute]
    public class DepartController : Controller
    {
        private readonly DeviceCtx _context;

        public DepartController(DeviceCtx context)
        {
            _context = context;
        }

        //列表主页
        public IActionResult Index()
        {
            return View(); 
        }

        //ajax 获取分页数据
        public AjaxResult list(int page , int limit,String name )
        {
            
            AjaxResult r = new AjaxResult();
            r.Count = _context.Depart.Where(p => p.Deleted == 0).Count();
            r.Code = 0;
            r.Data = _context.Depart.Where(p=>p.Deleted==0 && ((name==null)|| p.Name.Contains( name)))
                .OrderBy(p=>p.SortOrder)
                .Skip((page - 1)*limit).Take(limit)
                .ToList();

            return r;
        }
 

        /// <summary>
        /// 进入创建页面
        /// </summary>
        /// <returns></returns>
        public IActionResult Create()
        {
            return View();
        }

        
        [HttpPost] 
        public  AjaxResult doCreate([Bind("Name,SortOrder")] Depart depart)
        {
            var exist = _context.Depart.Any(p => p.Name == depart.Name && p.Deleted == 0);
            if (exist)
            {
                return AjaxResult.GetError("部门名称已经存在！");
            }

            depart.Deleted = 0;
            _context.Add(depart);
            _context.SaveChanges();
            return AjaxResult.GetOK("操作成功！");
        }

        /// <summary>
        /// 进入编辑页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<IActionResult> Edit(string id)
        {           
            var depart = await _context.Depart.FindAsync(id);           
            return View(depart);
        }

        /// <summary>
        /// 提交编辑
        /// </summary>        
        /// <returns></returns>
        [HttpPost]
        public AjaxResult DoEdit(string id,string name,int sortOrder)
        {
            var exist = _context.Depart.Any(p => p.Name == name && p.Id != id && p.Deleted == 0);
            if (exist)
            {
                return AjaxResult.GetError("部门名称已经存在！");
            }

            Depart depart = this._context.Depart.Single(p => p.Id == id);
            depart.Deleted = 0;
            depart.SortOrder = sortOrder;
            depart.Name = name;                        
            this._context.SaveChanges(); //保存

            return AjaxResult.GetOK("保存成功！");
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public AjaxResult Delete(string id)
        {
            AjaxResult result = new AjaxResult();
            if (id == null) return AjaxResult.GetError("删除失败！");

            result.Code = 1;
            var depart = _context.Depart.FirstOrDefault(m => m.Id == id);
            if (depart == null) return AjaxResult.GetError("未找到数据");

            depart.Deleted = 1;
            _context.SaveChanges();
            return AjaxResult.GetOK("删除成功");
        }
 
        private bool DepartExists(string id)
        {
            return _context.Depart.Any(e => e.Id == id);
        }
    }
}
